package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.OnePointDataBean;
import com.elinkway.infinitemovies.bean.OnePointItemBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by lxj on 2017/8/23.
 *
 * 广告请求数据解析类
 */

public class OnePointDataParser extends LVideoMobileParser<OnePointDataBean>{

    @Override
    public OnePointDataBean parse(JSONObject data) throws Exception {
        OnePointDataBean onePointDataBean = new OnePointDataBean();
        if(data == null){
            return null;
        }
        onePointDataBean.channel = data.optString("channel");
        onePointDataBean.fromid = data.optString("fromid");
        onePointDataBean.userid = data.optString("userid");
        JSONArray mArr = data.optJSONArray("data");
        if(mArr != null && mArr.length() > 0){
            onePointDataBean.itemBeanList = new ArrayList<OnePointItemBean>();
            OnePointItemBean itemBean = null;
            JSONObject itemObj = null;
            for (int i=0;i<mArr.length();i++){
                itemObj = mArr.optJSONObject(i);
                if(itemObj != null){
                    itemBean = new OnePointItemBean();
                    itemBean.ctype = itemObj.optString("ctype");
                    itemBean.impid = itemObj.optString("impid");
                    itemBean.pageid = itemObj.optString("pageid");
                    itemBean.docid = itemObj.optString("docid");
                    itemBean.title = itemObj.optString("title");
                    itemBean.dtype = itemObj.optString("dtype");
                    itemBean.date = itemObj.optString("date");
                    itemBean.summary = itemObj.optString("summary");
                    itemBean.image = itemObj.optString("image");
                    itemBean.imageType = itemObj.optString("imageType");
                    itemBean.source = itemObj.optString("source");
                    itemBean.url = itemObj.optString("url");
                    itemBean.up = itemObj.optString("up");
                    itemBean.down = itemObj.optString("down");
                    itemBean.play_count = itemObj.optString("play_count");
                    itemBean.comment_count = itemObj.optString("comment_count");
                    itemBean.comment_url = itemObj.optString("comment_url");
                    itemBean.template = itemObj.optString("template");
                    itemBean.deepLinkUrl = itemObj.optString("deepLinkUrl");
                    itemBean.pn = itemObj.optString("pn");
                    itemBean.actionUrl = itemObj.optString("actionUrl");
                    try {
                        itemBean.duration = itemObj.optInt("duration");
                    }catch (Exception e){

                    }

                    JSONObject wemediaObj = itemObj.optJSONObject("wemedia_info");
                    if(wemediaObj != null){
                        itemBean.wemedia_info.fromId = wemediaObj.optString("fromId");
                        itemBean.wemedia_info.image = wemediaObj.optString("image");
                        itemBean.wemedia_info.name = wemediaObj.optString("name");
                        itemBean.wemedia_info.type = wemediaObj.optString("type");
                        itemBean.wemedia_info.wemedia_url = wemediaObj.optString("wemedia_url");
                    }

                    JSONArray imageArr = itemObj.optJSONArray("image_urls");
                    if(imageArr != null && imageArr.length() > 0){
                        itemBean.image_urls = new ArrayList<>();
                        for (int j=0;j<imageArr.length();j++){
                            itemBean.image_urls.add(imageArr.optString(j));
                        }
                    }
                    JSONArray clickArr = itemObj.optJSONArray("clickMonitorUrls");
                    if(clickArr != null && clickArr.length() > 0){
                        itemBean.clickMonitorUrls = new ArrayList<>();
                        for (int j=0;j<clickArr.length();j++){
                            itemBean.clickMonitorUrls.add(clickArr.optString(j));
                        }
                    }
                    JSONArray viewArr = itemObj.optJSONArray("viewMonitorUrls");
                    if(viewArr != null && viewArr.length() > 0){
                        itemBean.viewMonitorUrls = new ArrayList<>();
                        for (int j=0;j<viewArr.length();j++){
                            itemBean.viewMonitorUrls.add(viewArr.optString(j));
                        }
                    }
                    JSONArray stdArr = itemObj.optJSONArray("stdMonitorUrls");
                    if(stdArr != null && stdArr.length() > 0){
                        itemBean.stdMonitorUrls = new ArrayList<>();
                        for (int j=0;j<stdArr.length();j++){
                            itemBean.stdMonitorUrls.add(stdArr.optString(j));
                        }
                    }
                    JSONArray fidArr = itemObj.optJSONArray("fidMonitorUrls");
                    if(fidArr != null && fidArr.length() > 0){
                        itemBean.fidMonitorUrls = new ArrayList<>();
                        for (int j=0;j<fidArr.length();j++){
                            itemBean.fidMonitorUrls.add(fidArr.optString(j));
                        }
                    }
                    onePointDataBean.itemBeanList.add(itemBean);
                }
            }
        }
        return onePointDataBean;
    }

}
